package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.system.dao.MemberEquitiesUseRepository;
import com.ssy.lingxi.system.dto.request.MemberEquitiesUseUpdateRequest;
import com.ssy.lingxi.system.dto.response.MemberEquitiesUseResponse;
import com.ssy.lingxi.system.entity.MemberEquitiesUse;
import com.ssy.lingxi.system.service.MemberEquitiesUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class MemberEquitiesUseServiceImpl implements MemberEquitiesUseService {
    @Autowired
    MemberEquitiesUseRepository memberEquitiesUseRepository;


    @Override
    public Wrapper<PageData<MemberEquitiesUseResponse>> memberEquitiesUseList(PageModel request) {
        Long count = memberEquitiesUseRepository.memberEquitiesUseCount();
        List<MemberEquitiesUseResponse> memberEquitiesUseResponses =null;
        if(0<count){
            memberEquitiesUseResponses = memberEquitiesUseRepository.memberEquitiesUseList(PageRequest.of(request.getCurrent(),request.getPageSize(), Sort.by("id").ascending()));
        }
        return Wrapper.success(new PageData(count,memberEquitiesUseResponses));
    }
    @Transactional
    @Override
    public Wrapper memberEquitiesUseUpdate(MemberEquitiesUseUpdateRequest request) {
        Optional<MemberEquitiesUse> byId = memberEquitiesUseRepository.findById(request.getId());
        if(!byId.isPresent()||null==byId.get().getId()){
            return Wrapper.fail("修改的会员使用设置不存在");
        }
        MemberEquitiesUse memberEquitiesUse = byId.get();
        memberEquitiesUse.setState(request.getState());
        memberEquitiesUse.setUpdateTime(System.currentTimeMillis());
        memberEquitiesUseRepository.save(memberEquitiesUse);
        return Wrapper.success();
    }


}
